package com.ruoyi.web.controller.mdm;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mdm.domain.SkuProductMaster;
import com.ruoyi.mdm.service.ISkuProductMasterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * spu主Controller
 *
 * @author shiseido
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/mdm/skuProductMaster")
public class SkuProductMasterController extends BaseController {
    @Autowired
    private ISkuProductMasterService skuProductMasterService;

    /**
     * 查询spu主列表
     */
    @ApiOperation("查询spu管理列表")
    @GetMapping("/list")
    public TableDataInfo list(SkuProductMaster skuProductMaster) {
        startPage();
        List<SkuProductMaster> list = skuProductMasterService.selectSkuProductMasterList(skuProductMaster);
        return getDataTable(list);
    }

    /**
     * 导出spu主列表
     */
    @ApiOperation("查询spu管理列表")
    @PreAuthorize("@ss.hasPermi('mdm:skuProductMaster:export')")
    @Log(title = "SPU管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SkuProductMaster skuProductMaster) {
        List<SkuProductMaster> list = skuProductMasterService.selectSkuProductMasterList(skuProductMaster);
        ExcelUtil<SkuProductMaster> util = new ExcelUtil<SkuProductMaster>(SkuProductMaster.class);
        util.exportExcel(response, list, "spu主数据");
    }

    /**
     * 获取spu主详细信息
     */
    @ApiOperation("获取spu主详细信息")
    @PreAuthorize("@ss.hasPermi('mdm:skuProductMaster:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(skuProductMasterService.selectSkuProductMasterById(id));
    }

    /**
     * 新增spu主
     */
    @ApiOperation("新增spu主")
    @PreAuthorize("@ss.hasPermi('mdm:skuProductMaster:add')")
    @Log(title = "SPU管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SkuProductMaster skuProductMaster) {

        // 来源 默认02:手动
        skuProductMaster.setSpuSource("02");

        skuProductMaster.setCreateBy(getUsername());
        skuProductMaster.setUpdateBy(getUsername());
        return toAjax(skuProductMasterService.insertSkuProductMaster(skuProductMaster));
    }

    /**
     * 修改spu主
     */
    @ApiOperation("修改spu管理")
    @PreAuthorize("@ss.hasPermi('mdm:skuProductMaster:edit')")
    @Log(title = "SPU管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SkuProductMaster skuProductMaster) {
        skuProductMaster.setUpdateBy(getUsername());
        return toAjax(skuProductMasterService.updateSkuProductMaster(skuProductMaster));
    }

    /**
     * 删除spu主
     */
    @ApiOperation("删除spu管理")
    @PreAuthorize("@ss.hasPermi('mdm:skuProductMaster:remove')")
    @Log(title = "SPU管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(skuProductMasterService.deleteSkuProductMasterByIds(ids));
    }

}
